﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace quanlysinhvien
{
    public partial class FormAdd_Student : Form
    {
        SqlConnection conn;
        SqlCommand cmd;
        SqlDataReader rd;
        public FormAdd_Student()
        {
            InitializeComponent();
        }       
        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (txtStuCode.Text == "" || txtStuName.Text == "" || txtCurAdd.Text == "" || txtNatPlace.Text == "" || txtConAdd.Text == "" || txtFatName.Text == "" || txtFatJob.Text == "" || txtMotName.Text == "" || txtMotJob.Text == "")
            {
                MessageBox.Show("Please check again");
            }
            else
            {
                string faculty = cmFaculty.Text;
                string clacode = cmLoadclass.Text;
                conn = Connect.getConnection();
                conn.Open();
                String strsql = "insert into TblStudents values ('" + txtStuCode.Text + "',N'" + clacode + "',N'" + faculty + "',N'" + txtStuName.Text + "','" + dateDOB.Text + "',N'" + comboSex.Text + "',N'" + txtCurAdd.Text + "',N'" + txtNatPlace.Text + "',N'" + txtConAdd.Text + "',N'" + txtFatName.Text + "',N'" + txtFatJob.Text + "',N'" + txtMotName.Text + "',N'" + txtMotJob.Text + "')";
                MessageBox.Show(strsql);
                cmd = new SqlCommand(strsql, conn);
                rd = cmd.ExecuteReader();
                conn.Close();
            }
        }    
        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Hide();            
        }
        private void FormAdd_Student_Load(object sender, EventArgs e)
        {
            loadcmfaculty();
        }

        private void cmFaculty_SelectedIndexChanged(object sender, EventArgs e)
        {
            loadcmclass();
        }
        private void loadcmfaculty()
        {
            conn = Connect.getConnection();
            conn.Open();
            SqlCommand cmd = new SqlCommand("select * from TblFaculty", conn);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            cmFaculty.DataSource = dt;
            cmFaculty.ValueMember = "Faculty_Code";
            cmFaculty.DisplayMember = "Faculty_Name";
            conn.Close();
            cmFaculty.Text = "Select faculty";
        }
        private void loadcmclass()
        {
            string faculty1 = cmFaculty.SelectedValue.ToString();
            conn = Connect.getConnection();
            conn.Open();
            string sql = "select * from TblClass where Faculty_Code ='" + faculty1 + "'";
            cmd = new SqlCommand(sql, conn);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            cmLoadclass.DataSource = dt;
            cmLoadclass.DisplayMember = "Class_Code";
            cmLoadclass.ValueMember = "Class_Code";
            conn.Close();
            cmLoadclass.Text = "Select class";            
        }

    }
}
